﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Caching;

namespace Alos.Common
{
    /// <summary>
    /// 缓存相关的操作类
    /// Copyright (C) PeiZheng
    /// </summary>
    public class DataCache
    {
        #region 获取当前应用程序指定CacheKey的Cache值
        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <returns></returns>
        public static object GetCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[CacheKey];
        }
        #endregion

        #region 设置当前应用程序指定CacheKey的Cache值
        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache(string CacheKey, object objObject)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            if (objObject == null)
                RemoveCache(CacheKey);
            objCache.Insert(CacheKey, objObject);
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey">键值</param>
        /// <param name="objObject">值</param>
        /// <param name="absoluteExpiration">缓存时间</param>
        /// <param name="slidingExpiration">弹性过期</param>
        ///  <param name="priority">优先级</param>
        ///  <param name="onRemoveCallback">回调函数</param>
        public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration, priority, onRemoveCallback);
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey">键值</param>
        /// <param name="objObject">值</param>
        /// <param name="absoluteExpiration">缓存时间</param>
        /// <param name="slidingExpiration">弹性过期</param>
        public static void SetCache(string CacheKey, object objObject, DateTime? absoluteExpiration, TimeSpan slidingExpiration)
        {
            if (objObject == null) return;
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject, null, absoluteExpiration ?? System.Web.Caching.Cache.NoAbsoluteExpiration, slidingExpiration);
        }
        #endregion

        #region 移除当前应用程序指定的CacheKey的Cache值
        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <returns></returns>
        public static void RemoveCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Remove(CacheKey);
        }
        /// <summary>
        /// 移除以指定字符串开头的缓存
        /// </summary>
        /// <param name="CacheKey"></param>
        public static void RemoveByPattern(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            IDictionaryEnumerator de = objCache.GetEnumerator();
            while (de.MoveNext())
            {
                if (de.Key.ToString().StartsWith(CacheKey))
                    objCache.Remove(de.Key.ToString());
            }
        }
        #endregion

        #region Set  设置Session值
        /// <summary>
        /// 设置Session值
        /// </summary>
        /// <param name="InfoName">Session名称</param>
        /// <param name="InfoValue">Session名称对应的值</param>
        static public void SetSession(String InfoName, String InfoValue)
        {
            string key = "E227453C-97A4-469D-8831-E0A1370CB9A3";
            InfoName = HashEncode.Md5Hash(key + InfoName);
            InfoValue = DEncrypt.Encrypt(InfoValue, key);
            HttpContext.Current.Session[InfoName] = InfoValue;
            if (InfoValue.Length == 0) HttpContext.Current.Session.Remove(InfoName);
        }
        /// <summary>
        /// 将一个对象写入Session
        /// </summary>
        /// <param name="InfoName"></param>
        /// <param name="InfoValue"></param>
        static public void SetSession(String InfoName, object InfoValue)
        {
            HttpContext.Current.Session[InfoName] = InfoValue;
            if (InfoValue == null) HttpContext.Current.Session.Remove(InfoName);
        }
        #endregion

        #region Get 取Session值
        /// <summary>
        /// 取Session值
        /// </summary>
        /// <param name="InfoName">Session名称</param>
        /// <returns>Session名称对应的值</returns>
        static public String GetSession(String InfoName)
        {
            string _Value = string.Empty;
            if (HttpContext.Current.Session[InfoName] != null)
            {
                _Value = Convert.ToString(HttpContext.Current.Session[InfoName]);
            }
            return _Value;
        }
        static public Object GetObjBySession(String InfoName)
        {
            object _Value = string.Empty;
            if (HttpContext.Current.Session[InfoName] != null)
            {
                _Value = HttpContext.Current.Session[InfoName];
            }
            return _Value;
        }
        #endregion

        #region 移出Session 的值
        static public void RemoveSession(String InfoName)
        {
            string _Value = string.Empty;
            string key = "E227453C-97A4-469D-8831-E0A1370CB9A3";
            InfoName = HashEncode.Md5Hash(key + InfoName);
            if (HttpContext.Current.Session[InfoName] != null)
            {
                HttpContext.Current.Session[InfoName] = null;
                HttpContext.Current.Session.Remove(InfoName);
            }
        }
        #endregion

        #region SetCookies值
        /// <summary>
        /// 添加一个 HttpCookie 对象
        /// </summary>
        /// <param name="cookie"></param>
        static public void SetCookies(HttpCookie cookie)
        {
            var xx = HttpContext.Current.Response;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>        
        /// 设置Cookies值        
        /// </summary>        
        /// <param name="InfoName">Cookies名称</param>        
        /// <param name="InfoValue">Cookies名称对应的值</param>        
        static public void SetCookies(String InfoName, String InfoValue)
        {
            SetCookies("AlosDefaultCookie", InfoName, InfoValue, 0);
        }
        /// <summary>        
        /// 设置Cookies值        
        /// </summary>        
        /// <param name="Index">Index</param>        
        /// <param name="InfoName">Cookies名称</param>        
        /// <param name="InfoValue">Cookies名称对应的值</param>        
        static public void SetCookies(string Index, String InfoName, String InfoValue) { SetCookies(Index, InfoName, InfoValue, 0); }
        /// <summary>        
        /// 设置Cookies值        
        /// </summary>        
        /// <param name="InfoName">Cookies名称</param>        
        /// <param name="InfoValue">Cookies名称对应的值</param>        
        /// <param name="Dates">有效期 天数</param>        
        static public void SetCookies(String InfoName, String InfoValue, int Dates) { SetCookies("AlosDefaultCookie", InfoName, InfoValue, Dates); }
        /// <summary>        
        /// 设置Cookies值        
        /// </summary>        
        /// <param name="Index">Index</param>        
        /// <param name="InfoName">Cookies名称</param>        
        /// <param name="InfoValue">Cookies名称对应的值</param>        
        /// <param name="Dates">有效期 天数</param>        
        static public void SetCookies(string Index, String InfoName, String InfoValue, int Dates)
        {
            string key = "94F90CB0-D4BC-46B3-9EDD-DA15A819C202";
            Index = HashEncode.Md5Hash(key + Index);
            InfoName = HashEncode.Md5Hash(key + InfoName);
            InfoValue = DEncrypt.Encrypt(InfoValue, key);
            if (Dates > 0) HttpContext.Current.Response.Cookies[Index].Expires = DateTime.Now.AddDays(Dates);
            HttpContext.Current.Response.Cookies[Index][InfoName] = InfoValue;
        }
        #endregion

        #region Get Cookies值
        /// <summary>
        /// 获取一个HttpCookie 对象
        /// </summary>
        /// <param name="cookieName"></param>
        /// <returns></returns>
        static public HttpCookie GetHttpCookie(String cookieName)
        {
            return HttpContext.Current.Request.Cookies[cookieName];
        }
        /// <summary>        
        /// 取Cookies值        
        /// </summary>        
        /// <param name="InfoName">Cookies名称</param>        
        /// <returns>Cookies名称对应的值</returns>        
        static public String GetCookies(String InfoName) { return GetCookies("AlosDefaultCookie", InfoName); }
        /// <summary>        
        /// 取Cookies值        
        /// </summary>        
        /// <param name="Index">Index</param>        
        /// <param name="InfoName">Cookies名称</param>        
        /// <returns>Cookies名称对应的值</returns>        
        static public String GetCookies(string Index, string InfoName)
        {
            string _value = string.Empty;
            string key = "94F90CB0-D4BC-46B3-9EDD-DA15A819C202";
            Index = HashEncode.Md5Hash(key + Index);
            InfoName = HashEncode.Md5Hash(key + InfoName);
            if (HttpContext.Current.Request.Cookies[Index] != null)
            {
                if (HttpContext.Current.Request.Cookies[Index][InfoName] != null)
                {
                    _value = DEncrypt.Decrypt(HttpContext.Current.Request.Cookies[Index][InfoName].ToString(), key);
                }
            }
            return _value;
        }
        #endregion
    }
}
